package com.cipta.lomob.adapter;

import java.util.LinkedList;

import com.cipta.lomob.R;
import com.cipta.lomob.components.CropCircleTransformation;
import com.cipta.lomob.model.Users;
import com.squareup.picasso.Picasso;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterPopularUser extends BaseAdapter{
	Activity activity;
	LinkedList<Users> listUser;
	LayoutInflater inflater;
	private View view;
	private PopularUserListener listener;
	
	public AdapterPopularUser(Activity activity, LinkedList<Users> listUser) {
		// TODO Auto-generated constructor stub
		this.activity = activity;
		this.listUser = listUser;
		inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public AdapterPopularUser(Activity activity, LinkedList<Users> listUser, PopularUserListener iListener){
		this.activity = activity;
		this.listUser = listUser;
		inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.listener = iListener;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listUser.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int arg0, final View arg1, final ViewGroup arg2) {
		// TODO Auto-generated method stub
		view = arg1;
		ViewHolder holder = null;
		if (view == null) {
			view = inflater.inflate(R.layout.inf_connected_friends, null);
			holder = new ViewHolder();
			holder.imgAvatar = (ImageView)view.findViewById(R.id.inf_img_connected_friends_avatar);
			holder.txtName = (TextView)view.findViewById(R.id.inf_txt_connected_friends_name);
			holder.txtTotalMomments = (TextView)view.findViewById(R.id.inf_txt_connected_friends_momments);
			holder.txtFollowers = (TextView)view.findViewById(R.id.inf_txt_connected_friends_followers);
			holder.txtFollowings = (TextView)view.findViewById(R.id.inf_txt_connected_friends_following);
			holder.btnAction = (Button)view.findViewById(R.id.btn_connected_friends_action);
			view.setTag(holder);
		} else {
			holder = (ViewHolder)view.getTag();
		}
		
		holder.txtName.setText(listUser.get(arg0).getName());
		holder.txtTotalMomments.setText("Momments "+listUser.get(arg0).getTotalMomments());
		holder.txtFollowers.setText("Followers "+listUser.get(arg0).getTotalFollowers());
		holder.txtFollowings.setText("Following "+listUser.get(arg0).getTotalFollowing());
		//set button image resource
		if(listUser.get(arg0).isFollowing()){
			holder.btnAction.setBackgroundResource(R.drawable.following_button);
		}else{
			holder.btnAction.setBackgroundResource(R.drawable.follow_button);
		}
		
		//set image
		Picasso.with(activity).load(listUser.get(arg0).getAvatar()).placeholder(R.drawable.avatar_my_profile).transform(new CropCircleTransformation()).into(holder.imgAvatar);
		holder.btnAction.setTag(view);
		//set action for button
		if(listener!=null){
			holder.btnAction.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
//					view = (View) v.getTag();
//					listener.onFollowClick(arg0, listUser.get(arg0), view, arg2);
					listener.onFollowClick(arg0, listUser.get(arg0), v, arg2);
				}
			});
		}
		return view;
	}
	
	static class ViewHolder{
		TextView txtName, txtTotalMomments, txtFollowers, txtFollowings;
		ImageView imgAvatar;
		Button btnAction;
	}
	
	public interface PopularUserListener{
		public void onFollowClick(int position, Users user, View v, ViewGroup viewGroup);
		
	}
}
